package com.tt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tt.entity.pojo.MovieRankBoxOfficePO;
import com.tt.entity.vo.MovieRankBoxOfficeVO;
import com.tt.mapper.MovieRankBoxOfficePOMapper;
import com.tt.mapper.MovieRankBoxOfficeVOMapper;
import com.tt.service.MovieRankBoxOfficeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 * 电影票房排行榜,需要定期更新 服务实现类
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:12
 */
@Service
public class MovieRankBoxOfficeServiceImpl extends ServiceImpl<MovieRankBoxOfficePOMapper, MovieRankBoxOfficePO> implements MovieRankBoxOfficeService {

    private final MovieRankBoxOfficeVOMapper movieRankBoxOfficeVOMapper;

    public MovieRankBoxOfficeServiceImpl(MovieRankBoxOfficeVOMapper movieRankBoxOfficeVOMapper) {
        this.movieRankBoxOfficeVOMapper = movieRankBoxOfficeVOMapper;
    }

    @Override
    public ArrayList<ArrayList<MovieRankBoxOfficeVO>> getBoxOffice() {
        ArrayList<MovieRankBoxOfficeVO> movieTotal;
        ArrayList<MovieRankBoxOfficeVO> movieNew;

        // 总榜
        QueryWrapper<MovieRankBoxOfficeVO> totalWrapper = new QueryWrapper();
        totalWrapper.select(MovieRankBoxOfficeVO.class, i -> !"day_box_office".equals(i.getColumn()))
                .isNotNull("total_box_office");
//                .orderByDesc("total_box_office"); // 这里实际是字符比较，因此排序结果有误
        movieTotal = new ArrayList<MovieRankBoxOfficeVO>(movieRankBoxOfficeVOMapper.selectList(totalWrapper));

        // 日榜
        QueryWrapper<MovieRankBoxOfficeVO> dayWrapper = new QueryWrapper();
        // 或者使用"total_score".equals(i.getProperty()) （配合@TableField("total_score")使用）
        dayWrapper.select(MovieRankBoxOfficeVO.class, i -> !"total_box_office".equals(i.getColumn()))
                .isNotNull("day_box_office");
//                .orderByDesc("day_box_office"); // 这里实际是字符比较，因此排序结果有误
        movieNew = new ArrayList<MovieRankBoxOfficeVO>(movieRankBoxOfficeVOMapper.selectList(dayWrapper));

        ArrayList<ArrayList<MovieRankBoxOfficeVO>> arrayLists = new ArrayList<>();
        arrayLists.add(movieTotal);
        arrayLists.add(movieNew);
        return arrayLists;
    }
}
